//
//  DBManager.cpp
//  airfight
//
//  Created by aaron on 12-11-24.
//  Copyright (c) 2012年 moonriver. All rights reserved.
//

#include "DBManager.h"

static string PLAYER_TABLE = "player";
static string BONUS_TABLE = "bonus";


DBManager* DBManager::s_SharedDBManager = NULL;

DBManager* DBManager::getInstance()
{
    static bool s_bFirstUsePrototypes = true;
    if (NULL == s_SharedDBManager) {
        s_SharedDBManager = new DBManager();
        s_bFirstUsePrototypes = false;
        if (!s_SharedDBManager->init())
        {
            return NULL;
        }
        
    }
    return s_SharedDBManager;
    
}

bool DBManager::init(void)
{
    dbName = "airfighter.db";
    string rootPath = CCFileUtils::sharedFileUtils()->getWriteablePath();
    string dbFilePath = rootPath + dbName;
    bool rs = initWithDBName(dbFilePath.c_str());
    return rs;

}

bool DBManager::initWithDBName(const char *dbName)
{
    result = sqlite3_open(dbName, &pDB);
    if(result != SQLITE_OK)
    {
        CCLog("打开数据库失败，错误码:%d ，错误原因:%s\n" , result, errMsg);
        return false;
    }
    return true;
    
}

DBManager::DBManager()
{
    
}

DBManager::~DBManager()
{
    
}

void DBManager::closeDB()
{
    sqlite3_close(pDB);
}

bool DBManager::tableIsExist(string tablename)
{
    bool tableIsExisted;
    string sql = "select count(type) from sqlite_master where type=\"table\" and name = \"";
    sql.append(tablename);
    sql.append("\"");
    result = sqlite3_exec(pDB,sql.c_str(),isExisted,&tableIsExisted,&errMsg);
    return tableIsExisted;
}

int DBManager::isExisted( void * para, int n_column, char ** column_value, char ** column_name )
{
    bool *isExisted_=(bool*)para;
    *isExisted_=(**column_value)!='0';
    return 0;
}

void DBManager::createTable(string sql,string name)
{
    result = sqlite3_exec(pDB,sql.c_str(),NULL,NULL,&errMsg);
    if( result != SQLITE_OK )
        CCLog( "创建表%s失败，错误码:%d ，错误原因:%s\n" , &name, result, errMsg );
}


//插入数据
void DBManager::insertData( string sql ){
    result = sqlite3_exec( pDB, sql.c_str() , NULL, NULL, &errMsg );
    if(result != SQLITE_OK )
        CCLog( "插入记录失败，错误码:%d ，错误原因:%s\n" , result, errMsg );
}


bool DBManager::initTables()
{
    string sql = "";
    if (!tableIsExist(PLAYER_TABLE))
    {
        sql = "create table ";
        sql.append(PLAYER_TABLE);
        sql.append("(playerid integer,username text,expr integer, grade integer,cash integer)");
        createTable(sql,PLAYER_TABLE);
    }
    if (!tableIsExist(BONUS_TABLE))
    {
        sql = "create table ";
        sql.append(BONUS_TABLE);
        sql.append("(playerid integer,bonusid integer,bonusnum integer)");
        createTable(sql,BONUS_TABLE);
    }
    return true;
}

bool DBManager::initDatas()
{
    string sql = "";
    if (tableIsExist(PLAYER_TABLE))
    {
        sql = "insert into ";
        sql.append(PLAYER_TABLE);
        sql.append("(playerid,username,expr,grade,cash) values (1,'aaron',10,1,99999)");
        insertData(sql);
    }
    
    if (tableIsExist(BONUS_TABLE))
    {
        sql = "insert into ";
        sql.append(BONUS_TABLE);
        sql.append("(playerid,bonusid,bonusnum) values (1,1,0)");
        insertData(sql);
        
        sql = "insert into ";
        sql.append(BONUS_TABLE);
        sql.append("(playerid,bonusid,bonusnum) values (1,2,1)");
        insertData(sql);
    }
    return true;
}

//getDataInfo的回调函数
int DBManager::loadPlayerRecord( void * para, int n_column, char ** column_value, char ** column_name )
{
    CCLog("playerid=%s,username=%s,expr=%s,grade=%s,cash=%s",column_value[0],column_value[1],column_value[2],column_value[3],column_value[4]);
    return 0;
}
//获取一条记录的信息 其中的pSend是一个实体类我们以后可以自定义一个继承了CCObject的类来代替他保存数据库中取出来的数据
void DBManager::getPlayerInfo( CCObject *pPlayer )
{
    string sql = "select * from ";
    sql.append(PLAYER_TABLE);
    sql.append(" where playerid = 1");
    sqlite3_exec( pDB, sql.c_str() , loadPlayerRecord, NULL, &errMsg );
}

